package com.wc._16届国特冲刺营._04动态规划_数位DP送分术.二进制问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/5/8 17:33
 * @description
 * https://www.lanqiao.cn/courses/51805/learning/?id=4072904&compatibility=false
 */
public class Main {
    /**
     * 思路：
     * 灵神的 数位DP 模板
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 70, M = 55;
    // 前 i 位置 有 j 个 1 的满足的个数
    static long[][] f = new long[N][N];
    static int[] nums = new int[N];
    static long L;
    static int k, n;

    public static void main(String[] args) {
        L = sc.nextLong();
        k = sc.nextInt();
        out.println(dp(L));
        out.flush();
    }

    static long dp(long x) {
        n = 0;
        while (x >> n != 0) n++;
        for (int i = 1; i <= n; i++) {
            Arrays.fill(f[i], -1);
            nums[i] = (int) (x >> (n - i) & 1);
        }
        return dfs(1, 0, true);
    }

    static long dfs(int i, int j, boolean limit) {
        if (i == n + 1) {
            return j == k ? 1 : 0;
        }

        if (!limit && f[i][j] >= 0) return f[i][j];
        int up = limit ? nums[i] : 1;
        long res = 0;

        for (int t = 0; t <= up; t++) {
            res += dfs(i + 1, j + t, limit && t == up);
        }

        if (!limit) f[i][j] = res;

        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

